using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using JGLib.States;

namespace TrafficTrouble
{
#if WINDOWS || XBOX
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main(string[] args)
        {
            using (Game game = new Game())
            {
                game.Run();
            }
        }
    }
#endif

    public class Game : Microsoft.Xna.Framework.Game
    {
        public static Game TrafficTroubleGame;

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public static int Width, Height;

        public Game()
        {
            TrafficTroubleGame = this;

            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            IsMouseVisible = true;

            TargetElapsedTime = TimeSpan.FromTicks(333333);
            InactiveSleepTime = TimeSpan.FromSeconds(1);
        }
        protected override void Initialize()
        {
            //JGLib.Settings.Storage.StartLoadStorageDevice(
            //    (device) => JGLib.Settings.XnaFile.Initialize(device, "TrafficTrouble")
            //);

            State.Initialize(this.Content);

            State.RegisterState(new States.LoadingState());
            State.RegisterState(new States.MainMenuState());
            State.RegisterState(new States.LevelSelectionState());
            State.RegisterState(new States.GameState());

            Game.Width = graphics.PreferredBackBufferWidth = 480;
            Game.Height = graphics.PreferredBackBufferHeight = 800;
            graphics.ApplyChanges();

            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            State.LoadStateContent();
        }
        protected override void UnloadContent() { }
        protected override void Update(GameTime gameTime)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                base.Exit();

            State.UpdateState(gameTime.ElapsedGameTime.TotalSeconds);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            this.spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            {
                State.DrawState(this.spriteBatch);
            }
            this.spriteBatch.End();

            base.Draw(gameTime);
        }

        public static void ExitGame()
        {
            TrafficTroubleGame.Exit();
        }

        public Texture2D GetScreenshot()
        {
            this.Draw(new GameTime());

            RenderTarget2D screenshot = new RenderTarget2D(this.GraphicsDevice, Game.Width, Game.Height);
            Color[] backBufferData = new Color[Game.Width * Game.Height];
            this.GraphicsDevice.GetBackBufferData<Color>(backBufferData);
            screenshot.SetData<Color>(backBufferData);

            return screenshot;
        }
    }
}
